<?php
/**
 * Author:Arthur
 * Date:2013-5-6
 * File Name:getUpdateStatus.php
 * Email:kingarthurx@sina.cn
 */
  require_once (dirname(__FILE__)."/../config/config.php");
  		
  
  if($_GET['method']=="update"){
  	 if(isset($_GET['mac'])){
	  	$sql1="select * from zy_equipment where MacAddress='".$_GET['mac']."'";
	  	$rss = mysql_query($sql1);
	  	$rows = mysql_fetch_array($rss);
	  }
  	 $unm = updateStatus($rows['updater']);
  	 if($unm == 1){
  	 	$error_message = "<result>success</result>\n";
  	 	outPutXml($error_message);
  	 }else{
  	 	$error_message = "<result>error</result>\n";
  	 	outPutXml($error_message);
  	 }
  }elseif($_GET['method']=="update_fullrouting"){
  		//执行全路由开通2个小时后代码
  		$content=update_fullrouting();
  		outPutXml($content);
  }else{
  	$content = getUpdateStatus();
  	outPutXml($content);
  }
  
  /**
   * 当API取出这条记录时,把routeupdate 更新为 0:未有更新记录
   */
  function updateStatus($user_id){
  		$sql="UPDATE zy_updatestatus set routeupdate=0 where user_id ='".$user_id."'";
  		$rs = mysql_query($sql);
  		if($rs > 0){
  			return 1;
  		}else{
  			return 0;
  		}
  		
  } 
 
  function getUpdateStatus(){
  	$content = "";
  		if(isset($_GET['mac'])){
	  	$sql1="select * from zy_equipment where MacAddress='".$_GET['mac']."'";
	  	$rss = mysql_query($sql1);
	  	$rows = mysql_fetch_array($rss);
 
  		$sql="select * from zy_updatestatus where user_id='".$rows['updater']."'";
  		$rs = mysql_query($sql);
  		$row = mysql_fetch_array($rs);
  		
  		$content.= "<result>\n";
		$content.= "<routeupdate>".$row['routeupdate']."</routeupdate>\n";
		$content.= "</result>\n";
	}
  	return $content;
  } 
  
  function update_fullrouting(){
  
  	if(isset($_GET['mac']))
  	{
  		$sql1="select * from zy_equipment where MacAddress='".$_GET['mac']."'";
  		$rss = mysql_query($sql1);
  		$rows1 = mysql_fetch_array($rss);
  		$updater=$rows1['updater'];
  		/*查询zy_full_routing表,判断全路由开启是否大于2个小时 .如果大于2个小时   将全路由关闭  并将该关闭状态在 zy_updatestatus记录 */
  		$sql1="select * from zy_full_routing where user_id='".$rows1['updater']."'";
  		$rss = mysql_query($sql1);
  		$rows = mysql_fetch_array($rss);  		
  		$date_min = date("Y-m-d H:i:s", mktime(date("H")-2, date("i"), date("s"), date("m"), date("d"), date("Y")));
  		if ($rows['mark']==1&&$date_min > $rows['open_time']) 
		{
			$sql="UPDATE zy_full_routing set mark=0,open_time='".date("0000-00-00 00:00:00")."' where user_id ='".$updater."'";
		  	$rs = mysql_query($sql);

			/*查询zy_updatestatus表中是否存在该用户的记录  不存在添加数据  否则仅仅修改routeupdate状态*/
			$sql="select * from zy_updatestatus where user_id='".$updater."'";
	  		$rs = mysql_query($sql);
	  		if($row = mysql_fetch_array($rs))
	  		{
			  	$sql="UPDATE zy_updatestatus set routeupdate=1 where user_id ='".$rows1['updater']."'";
			  	$rs = mysql_query($sql);
				if($rs > 0){
		  			return 1;
		  		}else{
		  			return 0;
		  		}
				
	  		}else{
		  		$sql="SELECT * FROM zy_user where user_id='$updater'";
		  		$rs = mysql_query($sql);
		  		$rows = mysql_fetch_array($rs);
		  		$user_name=$rows['user_name'];	  		
		  		$sql="insert into `zy_updatestatus`(`user_id`,`dnsupdate`,`routeupdate`,`ssidupdate`,`serverupdate`,`is_delete`,`crate_time`,`crate_user`) values ( '$updater','0','1','0','0','0','" . date("Y-m-d H:i:m") . "','$user_name')";
			  	$rs = mysql_query($sql);
			  	if($rs > 0){
		  			return 1;
		  		}else{
		  			return 0;
		  		}
	  		}
		}

  	}
  	
  }
  function outPutXml($content) {
	header("Content-type:text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n";
	echo "<netcuberesult>\n";
	echo $content;
	echo "</netcuberesult>";
	die();
}
